﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="QueryParser.cs" company="Microsoft">
//   Microsoft
// </copyright>
// <summary>
//   Defines the QueryParser type.
// </summary>
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// --------------------------------------------------------------------------------------------------------------------

namespace MySemanticSearch.Web.QueryBuilder
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text.RegularExpressions;

    using MySemanticSearch.Web.QueryBuilder.Conditions;

    /// <summary>
    /// Parses strings and creates a query in SQL
    /// </summary>
    public class QueryParser
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="QueryParser"/> class.
        /// </summary>
        /// <param name="builder">
        /// The builder.
        /// </param>
        /// <param name="fileTable">
        /// The file Table.
        /// </param>
        public QueryParser(ConditionBuilder builder, string fileTable)
        {
            this.Builder = builder;
            this.FileTable = fileTable;
        }

        /// <summary>
        /// Gets or sets Builder.
        /// </summary>
        private ConditionBuilder Builder { get; set; }

        /// <summary>
        /// Gets or sets FileTable.
        /// </summary>
        private string FileTable { get; set; }

        /// <summary>
        /// Parses a query from a given string
        /// </summary>
        /// <param name="input">
        /// The input.
        /// </param>
        /// <returns>
        /// an SQL query in a string
        /// </returns>
        public string ParseQuery(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }

            return this.ParseQueryReal(input).GetCondition();
        }

        /// <summary>
        /// Recursive method to parse conditions in a given string
        /// </summary>
        /// <param name="input">
        /// The input.
        /// </param>
        /// <returns>
        /// a condition
        /// </returns>
        private ICondition ParseQueryReal(string input)
        {
            var term = input.Split(';')[0];

            var condition = this.GetConditionFromString(term);

            if (input.Split(';').Length > 1)
            {
                input = input.Remove(0, term.Length);
                input = input.TrimStart(';');
                return this.Builder.HandleAndCondition(condition, this.ParseQueryReal(input));
            }

            return condition;
        }

        /// <summary>
        /// Parses a ginven string to create a codition
        /// </summary>
        /// <param name="input">
        /// The input.
        /// </param>
        /// <returns>
        /// a condition
        /// </returns>
        private ICondition GetConditionFromString(string input)
        {
            var matches = Regex.Match(input, App_Data.ConditionValidation.DocPropValidation);
            if (matches.Success)
            {
                return this.Builder.HandleDocumentPropertyCondition(matches.Groups[1].Value, matches.Groups[2].Value);
            }

            matches = Regex.Match(input, App_Data.ConditionValidation.TagValidation);

            if (matches.Success)
            {
                var taglist = new LinkedList<string>();

                var tags = matches.Groups[1].Value.Split(',');

                foreach (var tag in tags)
                {
                    taglist.AddFirst(tag);
                }

                return this.Builder.HandleTagCondition(this.FileTable, taglist);
            }

            matches = Regex.Match(input, App_Data.ConditionValidation.FullTextValidation);
            if (matches.Success)
            {
                return this.Builder.HandleFullTextCondition(matches.Groups[1].Value);
            }

            return null;
        }
    }
}
